package pers.jl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import pers.jl.common.BaseContext;
import pers.jl.entity.ShoppingCart;
import pers.jl.mapper.ShoppingCartMapper;
import pers.jl.service.IShoppingCartService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author luge
 * @since 2023-04-22
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    /**
     * 根据当前用户查出所有购物车信息
     *
     * @return
     */
    @Override
    public List<ShoppingCart> getAllShopCart() {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        // 指定用户
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        // 按照创建按时间的升序来排序
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        // 执行查询
        List<ShoppingCart> shoppingCartList = this.list(queryWrapper);
        return shoppingCartList;
    }

    /**
     * 加入购物车
     *
     * @param shopCart
     */
    @Override
    public void addToShopCart(ShoppingCart shopCart) {

        // 1. 先判断商品是菜品还是套餐
        if (shopCart.getDishId() == null) {
            // 是套餐
            // 再判断该商品在购物车中是否第一次加入
            LambdaQueryWrapper<ShoppingCart> setmealQueryWrapper = new LambdaQueryWrapper<>();
            // 查出当前用户是否存在该套餐
            setmealQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            setmealQueryWrapper.eq(ShoppingCart::getSetmealId, shopCart.getSetmealId());
            ShoppingCart shoppingCart = this.getOne(setmealQueryWrapper);
            // 第一次加入，数量设置为1
            if (StringUtils.isEmpty(shoppingCart)) {
                shopCart.setUserId(BaseContext.getCurrentId());
                shopCart.setNumber(1);
                shopCart.setCreateTime(LocalDateTime.now());
                // 新增
                this.save(shopCart);
            } else {
                // 不是第一次加入，查出原有对象对象，数量加一
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                this.updateById(shoppingCart);
            }
        } else {
            // 是菜品
            // 再判断该商品在购物车中是否第一次加入
            LambdaQueryWrapper<ShoppingCart> dishQueryWrapper = new LambdaQueryWrapper<>();
            // 查出当前用户是否存在该菜品
            dishQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            dishQueryWrapper.eq(ShoppingCart::getDishId, shopCart.getDishId());
            ShoppingCart shoppingCart = this.getOne(dishQueryWrapper);
            // 第一次加入，数量设置为1
            if (StringUtils.isEmpty(shoppingCart)) {
                shopCart.setUserId(BaseContext.getCurrentId());
                shopCart.setNumber(1);
                shopCart.setCreateTime(LocalDateTime.now());
                // 新增
                this.save(shopCart);
            } else {
                // 不是第一次加入，查出原有对象对象，数量加一
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                this.updateById(shoppingCart);
            }
        }
    }

    /**
     * 购物车商品数量减一
     *
     * @param shopCart
     */
    @Override
    public void deleteShopCart(ShoppingCart shopCart) {
        if (shopCart.getDishId() == null) {
            // 删除套餐
            LambdaQueryWrapper<ShoppingCart> setmealQueryWrapper = new LambdaQueryWrapper<>();
            // 指定用户
            setmealQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            // 指定套餐
            setmealQueryWrapper.eq(ShoppingCart::getSetmealId, shopCart.getSetmealId());
            ShoppingCart one = this.getOne(setmealQueryWrapper);
            if (one.getNumber() > 1) {
                // 数量大于等于1才能数量减一
                one.setNumber(one.getNumber() - 1);
                this.updateById(one);
            } else {
                // 只有一个，直接删除该商品
                this.removeById(one);
            }
        } else {
            // 删除菜品
            LambdaQueryWrapper<ShoppingCart> dishQueryWrapper = new LambdaQueryWrapper<>();
            // 指定用户
            dishQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            // 指定菜品
            dishQueryWrapper.eq(ShoppingCart::getDishId, shopCart.getDishId());
            ShoppingCart one = this.getOne(dishQueryWrapper);
            if (one.getNumber() > 1) {
                // 数量大于等于1才能数量减一
                one.setNumber(one.getNumber() - 1);
                this.updateById(one);
            } else {
                // 只有一个，直接删除该商品
                this.removeById(one);
            }
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteAllShopCart() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        // 指定用户
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        // 执行删除所有购物车信息
        this.remove(queryWrapper);
    }
}
